﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Guids;

namespace GuidGeneratorWPF
{
	public partial class GuidGeneratorControl : UserControl
	{
		public GuidGeneratorControl()
		{
			InitializeComponent();

			this.DataContext = Properties.Settings.Default;

			this.checkBoxStartWithWindows.DataContext = (App)App.Current;

			this.GenerateGuid();
		}

		public bool ShowTitleBar
		{
			get { return (bool)GetValue(ShowHeaderProperty); }
			set
			{
				SetValue(ShowHeaderProperty, value);
				this.TitleBar.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
			}
		}

		// Using a DependencyProperty as the backing store for ShowHeader.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty ShowHeaderProperty =
			 DependencyProperty.Register("ShowHeader", typeof(bool), typeof(GuidGeneratorControl), new UIPropertyMetadata(true));

		private void OnButtonGenerateGuid_Click(object sender, RoutedEventArgs e)
		{
			this.GenerateGuid();
		}

		private void OnButtonGenerateMultipleGuids_Click(object sender, RoutedEventArgs e)
		{
			string guids = App.GenerateMultipleGuids();

			this.textBoxCurrent.Text = guids;
			this.buttonCopy.IsEnabled = true;
		}

		private void OnButtonCopy_Click(object sender, RoutedEventArgs e)
		{
			Clipboard.SetText(this.textBoxCurrent.Text);
		}

		private void OnRadioButtonGuidFormat_Click(object sender, RoutedEventArgs e)
		{
			RadioButton radioButton = sender as RadioButton;
			if (radioButton == null)
				return;

			string tag = radioButton.Tag as string;
			if (tag == null)
				return;

			GuidFormats guidFormat;

			if (!Enum.TryParse<GuidFormats>(tag, out guidFormat))
				return;

			Properties.Settings.Default.GuidFormat = guidFormat;

			this.GenerateGuid();
		}

		private void GenerateGuid()
		{
			string guid = App.GenerateGuid();

			this.textBoxCurrent.Text = guid;
			this.buttonCopy.IsEnabled = true;
		}

		private void OnCheckBoxUpperCase_Checked(object sender, RoutedEventArgs e)
		{
			this.textBoxCurrent.Text = this.textBoxCurrent.Text.ToUpper();
			Clipboard.SetText(this.textBoxCurrent.Text);
		}

		private void OnCheckBoxUpperCase_Unchecked(object sender, RoutedEventArgs e)
		{
			this.textBoxCurrent.Text = this.textBoxCurrent.Text.ToLower();
			Clipboard.SetText(this.textBoxCurrent.Text);
		}
	}
}
